package linkedList;

import java.util.Iterator;
import java.util.function.Consumer;

/**
 * 单向链表 -带哨兵
 *
 * @author yyz
 * @create 2023-11-10 18:27
 */
public class DoublyLinkedListSentinel implements Iterable<Integer> {
    private final DoublyNode head;
    private final DoublyNode tail;

    /**
     * 在链表头后添加值
     *
     * @param value 待添加的值
     */
    public void addFirst(int value) {
        insert(0, value);
    }

    public DoublyLinkedListSentinel() {
        head = new DoublyNode(null, -1, null);
        tail = new DoublyNode(null, -2, null);
        head.next = tail;
        tail.prev = head;
    }

    /**
     * 在链表的尾部添加值
     *
     * @param value 待添加的值
     */
    public void addLast(int value) {
        DoublyNode prev = tail.prev;
        DoublyNode added = new DoublyNode(prev, value, tail);
        tail.prev = added;
        prev.next = added;
    }

    /**
     * 删除最后位置的节点
     */
    public void removeLast() {
        DoublyNode removed = tail.prev;
        if (removed == head) throw IllegalArgumentException(0);

        DoublyNode prev = removed.prev;
        prev.next = tail;
        tail.prev = prev;
    }

    /**
     * 获取指定索引的节点值
     *
     * @param index 索引
     * @return 节点值
     */
    public int get(int index) {
        DoublyNode node = findNode(index);
        if (node == null) throw IllegalArgumentException(index);
        return node.value;
    }

    /**
     * 删除第一个位置的节点
     */
    public void removeFirst() {
        remove(0);
    }

    /**
     * 根据索引删除节点
     *
     * @param index 索引
     */
    public void remove(int index) {
        DoublyNode pre = findNode(index - 1);
        if (pre == null) throw IllegalArgumentException(index);
        DoublyNode removed = pre.next;
        if (removed == tail) throw IllegalArgumentException(index);
        DoublyNode rear = removed.next;

        pre.next = rear;
        rear.prev = pre;
    }

    /**
     * 插入值到指定索引的节点
     *
     * @param index 索引
     * @param value 值
     */
    public void insert(int index, int value) {
        DoublyNode pre = findNode(index - 1);
        if (pre == null) throw IllegalArgumentException(index);

        DoublyNode rear = pre.next;
        DoublyNode inserted = new DoublyNode(pre, value, rear);
        pre.next = inserted;
        rear.prev = inserted;
    }

    /**
     * 索引异常
     *
     * @param index 索引位置
     * @return 异常
     */
    private static IllegalArgumentException IllegalArgumentException(int index) {
        return new IllegalArgumentException(String.format("index[%d] 不合法", index));
    }

    /**
     * 根据索引查找节点
     *
     * @param index 索引
     * @return 节点
     */
    private DoublyNode findNode(int index) {
        int i = -1;
        DoublyNode p;
        for (p = head; p != tail; p = p.next, i++) {
            if (index == i) {
                return p;
            }
        }
        return null;
    }


    /**
     * 函数遍历
     *
     * @param consumer 要执行的操作
     */
    public void loop(Consumer<Integer> consumer) {
        DoublyNode p = head.next;
        while (p != tail) {
            consumer.accept(p.value);
            p = p.next;
        }
    }

    /**
     * 迭代器遍历
     */
    @Override
    public Iterator<Integer> iterator() {

        return new Iterator<>() {
            DoublyNode p = head.next;

            @Override
            public boolean hasNext() {
                return p != tail;
            }

            @Override
            public Integer next() {
                int val = p.value;
                p = p.next;
                return val;
            }
        };
    }

}
